package com.supermap.census.ui;

import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.supermap.census.R;
import com.supermap.census.app.BaseActivity;
import com.supermap.census.app.SharedPreferencesManager;
import com.supermap.census.presenter.LoginPresenter;
import com.supermap.census.ui.intf.ILoginView;
import com.supermap.census.utils.IntentUtils;
import com.supermap.commonlibrary.utils.MyEditUtils;
import com.supermap.commonlibrary.utils.ToastUtil;

import butterknife.Bind;
import butterknife.OnClick;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity implements ILoginView {

    @Bind(R.id.return_img)
    ImageView returnImg;
    @Bind(R.id.userNameEd)
    EditText userNameEd;
    @Bind(R.id.passWordEd)
    EditText passWordEd;
    @Bind(R.id.ensureLoginBtn)
    Button ensureLoginBtn;
    private LoginPresenter mLoginPresenter;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_login;
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void doBusiness() {
        mLoginPresenter = new LoginPresenter(this);
    }

    @Override
    public String getUserName() {

        return MyEditUtils.getStr(userNameEd);
    }

    @Override
    public String getPassWord() {

        return MyEditUtils.getStr(passWordEd);
    }

    /**
     * 登录成功了
     */
    @Override
    public void loginSuccess() {

    }

    @Override
    public void successOrFalse(boolean state, String msg) {
        if (state) {
            SharedPreferencesManager.saveUserLogin(true);
            IntentUtils.into(this, MainHostActivity.class);
            finish();
        } else {
            SharedPreferencesManager.saveUserLogin(false);
        }
        ToastUtil.showToast(this, msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLoginPresenter.destory();
    }

    @Override
    public void showOrHiddenLoading(boolean flag) {

    }

    @OnClick({R.id.return_img, R.id.ensureLoginBtn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.return_img:
                finish();
                break;
            case R.id.ensureLoginBtn:
                /**
                 * 点击登录按钮了以后才调用这个方法
                 */
                mLoginPresenter.checkAccountMsg();
                break;
        }
    }
}
